/*!
 * @brief       	使能LP87702_WDI引脚的定时器，开启定时器中断和PWM，并设置占空比为50
 *
 * @param 				None
 *
 * @return				None
 */
void Enable_LP87702_WDI_TIM(void)
{
	HAL_TIM_Base_Start_IT(&htim3);
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
	__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_4,50);
}

/*!
 * @brief       	复位LP87702
 *
 * @param 				None
 *
 * @return				false/true: 错误/正确标志
 */
bool Reset_LP87702(void)
{
	uint8_t dat=0;
	dat=0x01;
	if(I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_RESET,1,&dat,1,true)!=true)
	{
		return false;
	}
	delay_ms(10);
	return true;
}

/*!
 * @brief       	控制LP87702的BUCK输出值
 *
 * @param 	[in]	buck_flag: BUCK序号，为2时控制所有
 *					[in]	voltage: 要输出的电压值
 *					[in]	WriteNotRead: 写入或读取
 *
 * @return				voltage_v: 写入或读取的电压值，控制所有时，写入的都为3.3V，读取的为两个电压值之和
 */
float Ctrl_LP87702_BUCK_VOUT(uint8_t buck_flag,float voltage_v,bool WriteNotRead)
{
	uint8_t dat=0;
	uint8_t add=0;	
	
	if(buck_flag>=2)
	{
		voltage_v=Ctrl_LP87702_BUCK_VOUT(0,3.3,WriteNotRead)+Ctrl_LP87702_BUCK_VOUT(1,3.3,WriteNotRead);
	}
	else
	{
		add = LP87702_BUCK0_VOUT+buck_flag;
		if(WriteNotRead)
		{
			
			if(voltage_v>=0.7f && voltage_v<=0.73f)
			{
				dat=0x14+(voltage_v-0.7f)/0.01f;
			}
			else if(voltage_v>0.73f && voltage_v<=1.4f)
			{
				dat=0x17+(voltage_v-0.73f)/0.005f;
			}
			else if(voltage_v>1.4f && voltage_v<=3.36f)
			{
				dat=0x9D+(voltage_v-1.4f)/0.02f;
			}
			else
			{
				dat=0;
			}
			
			I2C_Write_x(&hi2c2,LP87702_Slave_Add,add,1,&dat,1,true);
		}
		else
		{
			dat=I2C_Read_y(&hi2c2,LP87702_Slave_Add,add,1,1,true);	
			
			if(dat>=0x14 && dat<=0x17)
			{
				voltage_v=(dat-0x14)*0.01f+0.7f;
			}
			else if(dat>0x17 && dat<=0x9D)
			{
				voltage_v=(dat-0x17)*0.005f+0.73f;
			}
			else if(dat>=0x9D && dat<=0xFF)
			{
				voltage_v=(dat-0x9D)*0.02f+1.4f;
			}
			else
			{
				voltage_v=0;
			}
		}
	}	
	
	return voltage_v;
}

/*!
 * @brief       	开启LP87702的BUCK输出，这里是使能BUCK的输出
 *
 * @param 	[in]	buck_flag: BUCK序号，为2时控制所有
 *					[in]	EnableNotDisable: 使能或者关闭
 *
 * @return				None
 */
void Enable_LP87702_BUCK(uint8_t buck_flag,bool EnableNotDisable)
{
	uint8_t dat=0;
	uint8_t add=0;
	
	if(buck_flag>=2)
	{
		Enable_LP87702_BUCK(0,EnableNotDisable);
		Enable_LP87702_BUCK(1,EnableNotDisable);
	}
	else
	{	
		add = LP87702_BUCK0_CTRL_1+2*buck_flag;		
		dat=I2C_Read_y(&hi2c2,LP87702_Slave_Add,add,1,1,true);
		
		if(EnableNotDisable)
		{
			dat|=(1<<0);
		}
		else
		{
			dat&=~(1<<0);
		}
		
		I2C_Write_x(&hi2c4,LP87702_Slave_Add,add,1,&dat,1,true);
	}
}

/*!
 * @brief       	开启/关闭BOOST
 *
 * @param 	[in]	EnableNotDisable: 使能或者关闭
 *
 * @return				None
 */
void Enable_LP87702_BOOST(bool EnableNotDisable)
{
	uint8_t dat=0;
	
	dat=I2C_Read_y(&hi2c4,LP87702_Slave_Add,LP87702_BOOST_CTRL,1,1,true);	
	
	if(EnableNotDisable)
	{
		dat|=(1<<0);
	}
	else
	{	
		dat&=~(1<<0);
	}	
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_BOOST_CTRL,1,&dat,1,true);
}

/*!
 * @brief       	开启/关闭CLKIN引脚对看门狗的使能控制
 *
 * @param 	[in]	EnableNotDisable: 使能或者关闭
 *
 * @return				None
 */
void Enable_LP87702_WD_Pin_CON(bool EnableNotDisable)
{
	uint8_t dat=0;
	
	dat=0x87;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_WD_DIS_CTRL_CODE,1,&dat,1,true);
	dat=0x65;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_WD_DIS_CTRL_CODE,1,&dat,1,true);
	dat=0x1B;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_WD_DIS_CTRL_CODE,1,&dat,1,true);
	
	if(EnableNotDisable)
	{
		dat=0x01;
	}
	else
	{	
		dat=0x00;
	}
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_WD_DIS_CONTROL,1,&dat,1,true);
	
	dat=0xFF;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_WD_DIS_CTRL_CODE,1,&dat,1,true);
}

/*!
 * @brief       	初始化LP87702
 *
 * @param 				None
 *
 * @return				0/1: 错误/正确标志
 */
uint8_t Init_LP87702(void)
{
	uint8_t dat=0;
	
	Enable_VCC_5V;
	delay_ms(5);
	Enable_LP87702_EN;	
	delay_ms(5);
	
	if(!I2C_Judge(&hi2c4,LP87702_Slave_Add))
	{
		return 0;
	}
	
	if(!Reset_LP87702())
	{
		return 0;
	}
	
	dat=0x00;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_CONFIG,1,&dat,1,true);
	
	Enable_LP87702_WD_Pin_CON(true);
	
	dat=0x00;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_WD_CTRL_2,1,&dat,1,true);
	
	dat=0x08;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_BUCK0_CTRL_1,1,&dat,1,true);
	dat=0x1A;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_BUCK0_CTRL_2,1,&dat,1,true);
	dat=0x08;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_BUCK1_CTRL_1,1,&dat,1,true);
	dat=0x1A;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_BUCK1_CTRL_2,1,&dat,1,true);
	
	Ctrl_LP87702_BUCK_VOUT(0,1.8,true);
	Ctrl_LP87702_BUCK_VOUT(1,1.0,true);
	
	dat=0x58;
	I2C_Write_x(&hi2c4,LP87702_Slave_Add,LP87702_BOOST_CTRL,1,&dat,1,true);
	
	delay_ms(10);
	
	Enable_LP87702_BUCK(2,true);
	
	Enable_LP87702_BOOST(true);
	
	return 1;
}

/*!
 * @brief       	复位LP87524
 *
 * @param 				None
 *
 * @return				false/true: 错误/正确标志
 */
bool Reset_LP87524(void)
{
	uint8_t dat=0;
	dat=0x01;
	if(I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_RESET,1,&dat,1,true)!=true)
	{
		return false;
	}
	delay_ms(10);
	return true;
}

/*!
 * @brief       	控制LP87524的BUCK输出值，这里只控制VOUT，不控制FLOOR_VOUT
 *
 * @param 	[in]	buck_flag: BUCK序号，为4时控制所有
 *					[in]	voltage: 要输出的电压值
 *					[in]	WriteNotRead: 写入或读取
 *
 * @return				voltage_v: 写入或读取的电压值，控制所有时，写入的都为3.3V，读取的为四个电压值之和
 */
float Ctrl_LP87524_BUCK_VOUT(uint8_t buck_flag,float voltage_v,bool WriteNotRead)
{
	uint8_t dat=0;
	uint8_t add=0;	
	
	if(buck_flag>=4)
	{
		voltage_v=Ctrl_LP87524_BUCK_VOUT(0,3.3,WriteNotRead)+Ctrl_LP87524_BUCK_VOUT(1,3.3,WriteNotRead)+Ctrl_LP87524_BUCK_VOUT(2,3.3,WriteNotRead)+Ctrl_LP87524_BUCK_VOUT(3,3.3,WriteNotRead);
	}
	else
	{
		add = LP87524_BUCK0_VOUT+2*buck_flag;
		if(WriteNotRead)
		{
			
			if(voltage_v>=0.6f && voltage_v<=0.73f)
			{
				dat=0x0A+(voltage_v-0.6f)/0.01f;
			}
			else if(voltage_v>0.73f && voltage_v<=1.4f)
			{
				dat=0x17+(voltage_v-0.73f)/0.005f;
			}
			else if(voltage_v>1.4f && voltage_v<=3.36f)
			{
				dat=0x9D+(voltage_v-1.4f)/0.02f;
			}
			else
			{
				dat=0;
			}
			
			I2C_Write_x(&hi2c2,LP87524_Slave_Add,add,1,&dat,1,true);
		}
		else
		{
			dat=I2C_Read_y(&hi2c2,LP87524_Slave_Add,add,1,1,true);	
			
			if(dat>=0x0A && dat<=0x17)
			{
				voltage_v=(dat-0x0A)*0.01f+0.6f;
			}
			else if(dat>0x17 && dat<=0x9D)
			{
				voltage_v=(dat-0x17)*0.005f+0.73f;
			}
			else if(dat>=0x9D && dat<=0xFF)
			{
				voltage_v=(dat-0x9D)*0.02f+1.4f;
			}
			else
			{
				voltage_v=0;
			}
		}
	}	
	
	return voltage_v;
}

/*!
 * @brief       	开启LP87524的BUCK输出，这里是使能BUCK的输出，同时对应引脚置高电平
 *								初始化配置的是打开VOUT输出，如果要控制打开/关闭 VOUT/FLOOR_VOUT则需要另外配置
 *
 * @param 	[in]	buck_flag: BUCK序号，为4时控制所有
 *					[in]	EnableNotDisable: 使能或者关闭
 *
 * @return				None
 */
void Enable_LP87524_BUCK(uint8_t buck_flag,bool EnableNotDisable)
{
	uint8_t dat=0;
	uint8_t add=0;
	
	if(buck_flag>=4)
	{
		Enable_LP87524_BUCK(0,EnableNotDisable);
		Enable_LP87524_BUCK(1,EnableNotDisable);
		Enable_LP87524_BUCK(2,EnableNotDisable);
		Enable_LP87524_BUCK(3,EnableNotDisable);
	}
	else
	{	
		add = LP87524_BUCK0_CTRL1+2*buck_flag;		
		dat=I2C_Read_y(&hi2c2,LP87524_Slave_Add,add,1,1,true);
		
		if(EnableNotDisable)
		{
			dat|=(1<<7);
		}
		else
		{
			dat&=~(1<<7);
		}
		
		I2C_Write_x(&hi2c2,LP87524_Slave_Add,add,1,&dat,1,true);
		
		switch(buck_flag)
		{
			case 1:
			{			
				if(EnableNotDisable)
				{
					Enable_LP87524_EN1;
				}
				else
				{
					Disable_LP87524_EN1;
				}
				break;
			}
			case 2:
			{
				if(EnableNotDisable)
				{
					Enable_LP87524_EN2;
				}
				else
				{
					Disable_LP87524_EN2;
				}
				break;
			}
			case 3:
			{
				if(EnableNotDisable)
				{
					Enable_LP87524_EN3;
				}
				else
				{
					Disable_LP87524_EN3;
				}
				break;
			}
			default:break;
		}
	}
}

/*!
 * @brief       	初始化LP87524
 *
 * @param 				None
 *
 * @return				0/1: 错误/正确标志
 */
uint8_t Init_LP87524(void)
{
	uint8_t dat=0;
	
	Enable_VCC_5V;
	delay_ms(5);
	Disable_LP87524_EN1;
	Disable_LP87524_EN2;
	Disable_LP87524_EN3;
	delay_ms(5);
	
	if(!I2C_Judge(&hi2c2,LP87524_Slave_Add))
	{
		return 0;
	}
	
	if(!Reset_LP87524())
	{
		return 0;
	}
	
	dat=0x40;
	I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_CONFIG,1,&dat,1,true);
	
	dat=0x00;
	I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_PIN_FUNCTION,1,&dat,1,true);
	
	dat=0x34;
	I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_BUCK0_CTRL1,1,&dat,1,true);
	dat=0x44;
	I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_BUCK1_CTRL1,1,&dat,1,true);
	dat=0x54;
	I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_BUCK2_CTRL1,1,&dat,1,true);
	dat=0x64;
	I2C_Write_x(&hi2c2,LP87524_Slave_Add,LP87524_BUCK3_CTRL1,1,&dat,1,true);
	
	Ctrl_LP87524_BUCK_VOUT(0,3.3,true);
	Ctrl_LP87524_BUCK_VOUT(1,1.8,true);
	Ctrl_LP87524_BUCK_VOUT(2,1.2,true);
	Ctrl_LP87524_BUCK_VOUT(3,1.0,true);
	
	delay_ms(10);
	
	Enable_LP87524_BUCK(4,true);
	
	return 1;
}



